package com.superquan.selfstudybootmybatis.utils.gather;

import com.superquan.selfstudybootmybatis.entity.user.People;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 集合相关处理工具类
 */
@Component
public class GatherUtil {

    public static List<String> uniqListByCommon(List<String> list){
        //常规方法 两层for循环
        for(int i=0;i<list.size()-1;i++){
            for(int j=list.size()-1;j>i;j--){
                if(list.get(j).toString().equals(list.get(i).toString())){
                    list.remove(j);
                }
            }
        }
        return list;
    }

    /**
     * 使用set集合对list去重(对原list顺序无要求)
     * @param list
     * @return
     */
    public static List<String> uniqListByHashSet(List<String> list){
        //list转set会自动排序
        HashSet<String> set=new HashSet(list);
//      List<String> result = new ArrayList<>(set);
        //清空list集合内的元素
        list.clear();
        list.addAll(set);
        return list;
    }

    /**
     * 使用set集合对list去重(要求原list顺序不变)
     * @param list
     * @return
     */
    public static List<String> uniqListBySet(List<String> list){
        Set set=new HashSet();
        List<String> newList=new ArrayList<>();
        for(String arr:list){
            if(set.add(arr)){
                newList.add(arr);
            }
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    /**
     * 使用map对list去重(要求原list顺序不变)
     * @param list
     * @return
     */
    public static List<String> uniqListByMap(List<String> list){
        Map<String,Object> map=new HashMap<>();
        List<String> newList = new ArrayList<>();
        for(String arr:list){
            if(!map.containsKey(arr)){
                newList.add(arr);
                map.put(arr,arr);
            }
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    /**
     * 先用stream方法将集合转换成流，然后distinct去重，最后在将Stream流collect收集为List
     * @param list
     * @return
     */
    public static List<String> uniqListByStream(List<String> list){
        if(list.size()>0){
            List<String> newList = list.stream().distinct().collect(Collectors.toList());
            newList.forEach(System.out::println);
            return newList;
        }
        return null;
    }

    /**
     * 使用stream
     * @param list
     * @return
     */
    public static List<People> uniqListByStream2(List<People> list){
        ArrayList<People> newList = list.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(People::getName))), ArrayList::new
                )
        );
        return newList;
    }

    /**
     * 向list中间插入一个数据
     * @param list
     * @param data
     * @return
     */
    public static List<String> insertMiddle(List<String> list,String data){
        if(list.size()>0){
            System.out.println("插入前："+list.toString());
            int size = list.size();
            size=size/2;
            list.add(size,data);
            System.out.println("插入后："+list.toString());
            return list;
        }
        return null;
    }
}
